package model

import (
	"github.com/gomodule/redigo/redis"
	"strconv"
	"tizhi_front/nspel"
)

type Avatar struct {
	Id     int64  `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	PicUrl string `gorm:"column:pic_url"`
}

func (*Avatar) TableName() string {
	return "avatar"
}

type AvatarE struct{}

func (a *AvatarE) SelectAll() (avatars []Avatar, err error) {
	key := "avatar.all"
	err = nspel.RedisGetGob(key, &avatars)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "pic_url"}).Find(&avatars).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, avatars)
	return
}

type Avatar0 struct {
	PicUrl string `gorm:"column:pic_url"`
}

func (*Avatar0) TableName() string {
	return "avatar"
}

func (a *Avatar0) SelectById(id int64) (err error) {
	key := "avatar0.id:" + strconv.FormatInt(id, 10)
	err = nspel.RedisGetGob(key, a)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("pic_url").Where("id = ?", id).First(a).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, a)
	return err
}
